﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyStep.Business.Interfaces;
using EasyStep.Website.Models;
using System.Web.Security;
using EasyStep.Data.Entities;
using EasyStep.Business;

namespace EasyStep.Website.Controllers
{
    [Authorize]
    public class TestController : Controller
    {
        ITestProjectManager ProjectManager { get; set; }
        ITestUserManager UserManager { get; set; }
        ITestInfoManager TestManager { get; set; }
        ITestActionManager TestActionManager { get; set; }

        public TestController(ITestProjectManager projectManager, ITestUserManager userManager, ITestInfoManager testManager, ITestActionManager testActionManager)
        {
            ProjectManager = projectManager;
            UserManager = userManager;
            TestManager = testManager;
            TestActionManager = testActionManager;
        }

        public ActionResult Index()
        {
            var model = new TestListModel()
            {
                Projects = ProjectManager.GetUserProjects(new Guid(Membership.GetUser().ProviderUserKey.ToString()))
            };

            model.Projects.ToList().ForEach(p => { p.Tests = TestManager.GetProjectTests(p.Id); });

            return View(model);
        }

        public ActionResult Create()
        {
            ViewBag.UserProjects = new SelectList(ProjectManager.GetUserProjects(new Guid(Membership.GetUser().ProviderUserKey.ToString())), "Id", "Name", null);
            return View();
        }

        [HttpPost]
        public ActionResult Create(TestModel model, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                TestManager.Create(new TestInfo() { Name = model.Name, ProjectId = model.ProjectId, Description = model.Description });
                return RedirectToAction("Index");
            }
            else
                return View();
        }

        public ActionResult Details(Guid id)
        {
            var test = TestManager.Get(id);
            var model = ObjectMapper.ResolveMap<TestInfo, TestModel>(test);
            model.ProjectName = ProjectManager.Get(test.ProjectId).Name;
            
            ViewBag.UserProjects = new SelectList(ProjectManager.GetUserProjects(new Guid(Membership.GetUser().ProviderUserKey.ToString())), "Id", "Name", model.ProjectId);
            var a = TestActionManager.GetAll().ToList();
            return View(model);
        }

        [HttpPost]
        public ActionResult Details(TestModel model)
        {
            if (ModelState.IsValid)
            {
                TestManager.Update(ObjectMapper.ResolveMap<TestModel, TestInfo>(model));

                return RedirectToAction("Index");
            }
            else
                return View();
        }

        public JsonResult GetSequences()
        {
            List<TestActionModel> toSerialize = new List<TestActionModel>();
            TestActionManager.GetAll().ToList().ForEach(testAction => 
            { 
                toSerialize.Add(ObjectMapper.ResolveMap<EasyStep.Attributes.TestAction, TestActionModel>(testAction)); 
            });

            return Json(toSerialize, JsonRequestBehavior.AllowGet);
        }

        /*public JsonResult GetTestData(Guid id)
        {

        }*/



        public JsonResult TestSteps(Guid id)
        {
            var test = TestManager.Get(id);
            var testProject = ProjectManager.Get(test.ProjectId);
            if (UserManager.Authorize(testProject))
            {
                return Json(test.Steps.ToArray(),JsonRequestBehavior.AllowGet);
            }
            else
            {
                throw new AccessViolationException("can't show test steps for current user!"+id.ToString()+",user:"+UserManager.Current().Id);
            }
        }
        
        public ActionResult Delete(Guid id)
        {
            var test = TestManager.Get(id);
            var testProject = ProjectManager.Get(test.ProjectId);
            if (UserManager.Authorize(testProject))
            {
                TestManager.Delete(test);
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("NotAuthorized", "Account");
            }
        }       
    }
}